package com.kehongborui.fliggy.util.spider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-09-11
 */
public class ScoreSubmitUtil {
    private static String LOGIN_URL = "http://10.3.2.218:8080/login/checkLogin";
    private static String COURSE_COMMENT_LIST_URL = "http://10.3.2.218:8080/course/loadDataComment";
    private static String ADD_COURSE_URL = "http://10.3.2.218:8080/course/addCourse";
    public static void main(String[] args) throws IOException {
        login();

        // 增加当天课程
        addCourse();
        // 根据当天课程评分

        // 获取添加课程后的课程id
        Day day = lastCourseDay();
        // 修改学院分数
        List<Map> stuList = getStuList(day.getId());
        stuList.stream().forEach(e -> {
            comment(e, day.getId(), 85);
        });
    }

    /**
     * 用户登录
     * @throws IOException
     */
    public static void login() throws IOException {
        // 调用 CookieManager 管理 HttpURLConnection 的cookie
        CookieManager manager = new CookieManager();
        CookieHandler.setDefault(manager);
        //设置cookie策略，只接受与你对话服务器的cookie，而不接收Internet上其它服务器发送的cookie
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        Map userinfo = new HashMap();
        userinfo.put("name", "张彭洋明");
        userinfo.put("password", "zpymkhbr2");

        // 登录
        Map form = HttpClient.form(LOGIN_URL, userinfo);
        System.out.println(form);
    }

    /**
     * 获取学生列表信息
     * @param courseId
     * @return
     * @throws IOException
     */
    public static List<Map> getStuList(Integer courseId) throws IOException {
        Map courseInfo = new HashMap();
        courseInfo.put("courseId", courseId);
        Map form = HttpClient.form(COURSE_COMMENT_LIST_URL, courseInfo);
        JSONArray stus = (JSONArray) JSON.toJavaObject((JSONObject) form.get("data"), Map.class).get("list");

         return stus.stream().map(e -> {
            return JSON.toJavaObject((JSONObject) e, Map.class);
        }).collect(Collectors.toList());
    }

    /**
     * 提交学生成绩
     * @param stu
     */
    public static void comment(Map stu, Integer courseId, Integer score) {
        if ("龙春宇".equals((String)stu.get("studentName")) ||
                "郭凯敏".equals((String)stu.get("studentName")) ||
                "张城".equals((String)stu.get("studentName")) ||
                "王松".equals((String)stu.get("studentName"))) {
            return;
        }
        Map stuInto = new HashMap();
        stuInto.put("courseId", courseId);
        stuInto.put("studentName", stu.get("studentName"));
        stuInto.put("teacherComment", "还可以哦，不错哦，加油哦");
        stuInto.put("teacherScore", score);

        try {
            Map form = HttpClient.form("http://10.3.2.218:8080/course/teacherComment", stuInto);
            System.out.println(form);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加课程
     * @throws IOException
     */
    public static void addCourse() throws IOException {
        Map courseInto = new HashMap();
        courseInto.put("day", currentDay().getDay());  // 天数
        courseInto.put("stage", "高级部分"); // 课程阶段
        courseInto.put("content", "飞猪项目管理后台"); // 课程内容
        courseInto.put("courseTime", new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())); // 课程时间
        courseInto.put("courseTime", "2018-09-14"); // 课程时间
        courseInto.put("remark", "飞猪项目管理后台");
        Map form = HttpClient.form(ADD_COURSE_URL, courseInto);
        System.out.println(form);
    }

    /**
     * 获取最近课程天数
     * @throws IOException
     */
    public static Day lastCourseDay() throws IOException {
        Map form = HttpClient.form("http://10.3.2.218:8080/course/loadData", null);
        Map map = JSON.toJavaObject(((JSONObject) form.get("data")), Map.class);
        JSONArray list = (JSONArray)map.get("list");
        List<Day> days = list.stream().map(e -> {
            Map t = JSON.toJavaObject((JSONObject) e, Map.class);
            Day day = new Day((String)t.get("day"), (Integer)t.get("id"));
            return day;
        }).collect(Collectors.toList());

        System.out.println(days);
        return days.get(days.size() - 1);
    }

    /**
     * 即将创建的课程天数
     * @return
     */
    public static Day currentDay() throws IOException {
        Day lastDay = lastCourseDay();
        lastDay.setDay("day" + (Integer.parseInt(lastDay.getDay().substring(3)) + 1));
        return lastDay;
    }

    static class Day {
        private String day;
        private Integer id;

        public Day(String day, Integer id) {
            this.day = day;
            this.id = id;
        }

        public String getDay() {
            return day;
        }

        public void setDay(String day) {
            this.day = day;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Day{" +
                    "day='" + day + '\'' +
                    ", id=" + id +
                    '}';
        }
    }
}
